/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package collections;
import java.util.*;

/**
 * This program helps you in storing the large amount of data as a collection.
 * The LinkedList is a part of collection that constructs a list containing the elements of the specified collection.
 * Iterator methods returns the values in the order in which they are stored.
 *
 * If you want to insert the data in the linkedList then use add() method.
 * The hasNext() method returns true if the iterator contains more elements and
 * the next() method returns the next element in the iteration.
 * To insert and remove the data at first, last and specified position in the linkedList, you use the addFirst(), addLast(), add(), removeFirst(), removeLast() and remove() methods.
 * To retrieve the element with respect to a specified position use the getFirst(), getLast() and get() methods.
 *
 * @author Iskandar Sarkawi
 */
public class LinkedListExample2 {

    public static void main(String[] args) {
        System.out.println("Linked List Example2");

        LinkedList<Integer> list = new LinkedList<Integer>();

        //Adding data in the list
        list.add(11);
        list.add(22);
        list.add(33);
        list.add(44);

        printList(list);
        listSize(list);

        System.out.println("Adding data at the 1st location: 55");
        //Adding first
        list.addFirst(55);        
        printList(list);
        listSize(list);

        System.out.println("Adding data at the last location: 66");
        //Adding first
        list.addLast(66);
        printList(list);
        listSize(list);

        System.out.println("Adding data at the third location: 99");
        //Add data at the 3rd location
        list.add(2, 99);
        printList(list);
        listSize(list);

        //retrieve 1st data
        System.out.println("First data: " + list.getFirst());

        //retrieve the last data
        System.out.println("Last data: " + list.getLast());

        //retrieve specific data
        System.out.println("Data at 4th location: " + list.get(3));

        //REMOVE DATA
        //Remove first
        int first = list.removeFirst();
        System.out.println("\nData removed from the 1st location: " + first);
        printList(list);
        listSize(list);

        //remove last
        int last = list.removeLast();
        System.out.println("Data removed from the last location: " + last);
        printList(list);
        listSize(list);

        //remove 2nd data
        int second = list.remove(1);
        System.out.println("Data removed from the 2nd location: " + second);
        printList(list);
        listSize(list);

        //remove all
        list.clear();
        printList(list);
        listSize(list);

    }

    /*
     * Prints all items in the list
     * @param Linked List
     */
    public static void printList(LinkedList<Integer> list){
        Iterator iterator;
        iterator = list.iterator();

        if(list.isEmpty())
            System.out.println("Linked list is empty");
        else{
            System.out.print("Now the list contain: ");
            while(iterator.hasNext())
                System.out.print(iterator.next() +" ");
            
            System.out.println();
        }
    }

    public static void listSize(LinkedList<Integer> list){
        if(list.isEmpty())
            System.out.println("Linked list is empty");
        else
            System.out.println("Now the list size is: " + list.size());
        System.out.println();
    }

    

}
